package rungekutta;

import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;

public class Problem extends RungeKutta {
    
    private Scanner in;      // input
    private PrintWriter out; // output

    private double[] c;      // heat capacities of the bodies
    private double[] q;      // powers of heat sources
    private int m1, m2;      // number of conductive and radiant connections between bodies
    private double[][] a, b; // coefficients of conductive and radiant connections between bodies
    private double dx, eps;  // time step and accuracy
    private int order;       // order of the Runge-Kutta method
    private double autoEps;

    /**
     * Constructor
     * @param fileName path to file with input data
     */
    public Problem(String fileName, int order, double step, double eps, double autoEps, boolean result) {
        try {
            in = new Scanner(new File(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
            System.exit(0);
        }
        try {
            out = new PrintWriter("output.txt");
        } catch (FileNotFoundException e) {
            System.out.println("Cannot create or modify \"output.txt\"");
            System.exit(0);
        }
        try {
            this.order = order;
            this.dx = step;
            this.eps = eps;
            this.autoEps = autoEps;
            this.result = result;
            loadData();
            solve(dx, eps, order, autoEps);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(64);
        }
    }

    /**
     * Reads the data from input file
     */
    private void loadData() {
        n = in.nextInt();
        y = new double[n + 1];
        y[0] = in.nextDouble();
        for (int i = 1; i <= n; i++) {
            y[i] = y[0];
        }
        c = new double[n + 1];
        for (int i = 1; i <= n; i++) {
            c[i] = in.nextDouble();
        }
        q = new double[n + 1];
        for (int i = 1; i <= n; i++) {
            q[i] = in.nextDouble();
        }
        a = new double[n + 1][n + 1];
        b = new double[n + 1][n + 1];
        for (int i = 0; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                a[i][j] = b[i][j] = 0;
            }
        }
        m1 = in.nextInt();
        for (int i = 0; i < m1; i++) {
            int u = in.nextInt();
            int v = in.nextInt();
            a[u][v] = a[v][u] = in.nextDouble();
        }
        m2 = in.nextInt();
        for (int i = 0; i < m2; i++) {
            int u = in.nextInt();
            int v = in.nextInt();
            b[u][v] = b[v][u] = in.nextDouble();
        }
    }

    protected double f(double x, double y, int i) {
        double r = this.q[i];
        for (int j = 0; j <= this.n; j++) {
            r -= this.a[i][j] * (y - this.y[j]) + 5.67e-8 * this.b[i][j] * (Math.pow(y, 4) - Math.pow(this.y[j], 4));
        }
        r /= c[i];
        return r;
    }

    protected void printNumber(double val, int len) {
        String s = String.format("%f", val);
        for (int i = 0; i < len - s.length(); i++) {
            out.print(" ");
        }
        out.print(s);
    }

    protected void processAnswer(double x, double[] y) {
        printNumber(x, 10);
        for (int i = 1; i <= this.n; i++) {
            out.print(" ");
            printNumber(y[i], 10);
        }
        out.println();
    }

}
